package com.liyuci.core.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.liyuci.core.exception.ErrorCode;

import java.io.Serializable;

import org.springframework.data.domain.Page;

/**
 * 
 * @author liyuci
 * @date 2018-02-07 22:22:57
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestPageResult implements Serializable {

	private static final long serialVersionUID = -3511012093385758421L;
	/**
     * 返回编码
     */
	@JSONField(ordinal = 1)
    private int code;

    /**
     * 返回消息
     */
	@JSONField(ordinal = 2)
    private String message;
	
	/**
	 * 第几页
	 */
	@JSONField(ordinal = 3)
    private int pageNum;

	/**
	 * 每页显示多少条数据
	 */
    @JSONField(ordinal = 4)
    private int pageSize;

    /**
     * 总页数
     */
    @JSONField(ordinal = 5)
    private long pages;

    /**
     * 总共多少条数据
     */
    @JSONField(ordinal = 6)
    private long total;
    
    /**
     * 返回数据
     */
	@JSONField(ordinal = 7, serialzeFeatures = SerializerFeature.WriteMapNullValue)
    private Object data;
    
    public RestPageResult() {
    	throw new UnsupportedOperationException();
    }
    
    public RestPageResult(int code, String message) {
        this.code = code;
        this.message = message;
    }
    
    public RestPageResult(int code, String message, int pageNum, int pageSize, long pages, long total, Object data)
    {
    	this.code = code;
    	this.message = message;
    	this.pageNum = pageNum;
    	this.pageSize = pageSize;
    	this.pages = pages;
    	this.total = total;
    	this.data = data;
    }
    
    public int getCode()
	{
		return code;
	}

	public void setCode(int code)
	{
		this.code = code;
	}

	public String getMessage()
	{
		return message;
	}

	public void setMessage(String message)
	{
		this.message = message;
	}

	public Object getData()
	{
		return data;
	}

	public void setData(Object data)
	{
		this.data = data;
	}

	public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
    
    public static RestPageResult ok(Page<?> page) {
        return new RestPageResult(ErrorCode.OK.getCode(), ErrorCode.OK.getMessage(), 
        		page.getNumber(), page.getSize(), page.getTotalPages(), 
        		page.getTotalElements(), page.getContent());
    }
    
    public static RestPageResult failure() {
        return new RestPageResult(ErrorCode.FAILURE.getCode(), ErrorCode.FAILURE.getMessage());
    }
    
    public static RestPageResult failure(ErrorCode errorCode) {
        return new RestPageResult(errorCode.getCode(), errorCode.getMessage());
    }
    
}
